/*选项卡*/
var gallery = null;

var jq = $.noConflict();

mui.init({
	swipeBack: false
});
var articlePage = 1; //精评分页
var newsLivePage = 1; //实时资讯
var bookPage = 1; //书刊

(function($) {

	//阻尼系数
	var deceleration = mui.os.ios ? 0.003 : 0.0009;
	$('.mui-scroll-wrapper').scroll({
		bounce: false,
		indicators: true, //是否显示滚动条
		deceleration: deceleration
	});
	//获得slider插件对象
	gallery = mui('#content');

	mui.each(mui("iframe"), function(index, item) {

		item.height = h.winInfo().height - 44;
		//console.log('item.height'+item.height );
	});

	//中诚精评

	function removeClass(obj, cls) {

		var reg = new RegExp('(\\s|^)' + cls + '(\\s|$)');
		obj.className = obj.className.replace(reg, ' ');

	}

	document.querySelector('#content').addEventListener('slide', function(event) {
		if(event.detail.slideNumber == 0) {

		} else if(event.detail.slideNumber == 1) {
			GetB();
		} else if(event.detail.slideNumber == 2) {
			SetLive();
			 
		} else if(event.detail.slideNumber == 3) {
			SetFin();
		}

		jq.each($(".biaoti"), function(index, item) {
			if(index == event.detail.slideNumber) {
				jq(this).addClass('active').siblings().removeClass("active");

				return false;

			}

			/*	$.each($("#biaoti li"), function(index,item) {
					$(this).addClass('active').siblings().removeClass("active");
					
					
				});*/

		});

	});

	/*mui.each(document.getElementsByClassName("biaoti"),function(index,item){
		
		mui.item.addEventListener('tap',function(){
			
			this.className+= " active";
		});
	});
	*/

	document.getElementById('link_a').addEventListener('tap', function() {

		//window.location.href = '#article';
		gallery.slider().gotoItem(0);
	});
	//实时资讯
	function SetLive() {
		var iframe=document.getElementById('iframeLivePage');
		if(iframe.src!='http://m.99990158.com/newlive.html') {
			iframe.src = 'http://m.99990158.com/newlive.html';
			iframe.style.height = h.winInfo().height - 44;
		}

		gallery.slider().gotoItem(2);
	}
	document.getElementById('link_live').addEventListener('tap', function() {
		SetLive()
		return;

		/*document.getElementById('iframeLivePage').src = 'http://m.99990158.com/newlive.html';
		document.getElementById('iframeLivePage').style.height = h.winInfo().height - 44;*/
		if(maxId == 0) {
			createLive(1);
		}
		//document.getElementById('LivePage').scrollTop=0;
		//gallery.slider().s(1)
		gallery.slider().gotoItem(2);

	});
	//财经日历
	function SetFin() {
		var iframe=document.getElementById('iframeFinPage');
		if(iframe.src=="") {
			var url = 'http://liveapi.99990158.com/rili_if.html';
			iframe.src = url;
			iframe.style.height = h.winInfo().height - 44;console.log()
		}

		gallery.slider().gotoItem(3);
	}
	document.getElementById('link_fin').addEventListener('tap', function() {
		SetFin();

	});
	//中诚书刊
	document.getElementById('link_book').addEventListener('tap', function() {
		GetB();
		gallery.slider().gotoItem(1);
	});

	GetA(1);

	function GetA(page) {
		$.getJSON('http://www.99990158.com/ajax/GetArticlePageList.aspx', {
			page: 1,
			size: 10,
			cid: 83,
			channel: 'news',
			sort: 'add_time desc'
		}, function(res) {

			$.each(res.data, function(index, item) {

				CreateA('ArticleList', item, 1);
			});

			pullToRefreshArticle();
		});

	}
	
	function GetB() {
		if(document.getElementById('BookPage').children.length>0)
		{
			return;
		}
		$.getJSON('http://www.99990158.com/ajax/GetArticlePageList.aspx', {
			page: 1,
			size: 10,
			cid: 84,
			channel: 'news',
			sort: 'add_time desc'
		}, function(res) {
			$.each(res.data, function(index, item) {

				CreateA('BookPage', item, 1);
			});
		});
	}

	function CreateA(id, item, fag) {
		/*var model = document.getElementById('model').innerHTML;
		 
		model.id = item.id;*/

		var modelHtml = document.getElementById('model').innerHTML;
		var html = modelHtml.replace('{{title}}', item.title);
		html = html.replace('{{source}}', item.source);
		html = html.replace('{{author}}', item.author);
		html = html.replace('{{time}}', item.add_time);

		html = html.replace('{{shoucangweixuan}}', item.appCollectionCount ? item.appCollectionCount : 0);

		html = html.replace('{{click}}', item.click);

		html = html.replace('{{zhuanfa}}', item.appShareCount ? item.appShareCount : 0);
		html = html.replace('{{id}}', item.id);

		var model = document.createElement('div');
		model.id = item.id;
		model.innerHTML = html;

		if(fag == 0 && !document.getElementById(item.id)) {
			document.getElementById(id).insertBefore(model, document.getElementById('ArticleList').firstChild);
		} else {
			//console.log(0);
			document.getElementById(id).appendChild(model);

		}
	}
	var maxId = 0;
	jq('#times').text(new XDate().toString('yyyy年MM月dd日 HH:mm:ss'));
	setInterval(function() {
		jq('#times').text(new XDate().toString('yyyy年MM月dd日 HH:mm:ss'));
	}, 1000);

	function getCurrData() {
		console.log(maxId);
		jq.getJSON('http://www.99990158.com/ajax/GetNewsLive4Now.ashx?page=1&maxId=' + maxId, function(rps) {

			if(rps.length > 0) {
				maxId = _.max(rps, function(chr) {
					return chr.id;
				}).id;

				jq.each(rps, function(index, item) {

					var timestamp3 = item.createdAt;
					var newDate = new Date();
					newDate.setTime(timestamp3 * 1000);

					var a = $(item.contentHtml);

					a.removeAttr('href');
					a.find('a').removeAttr('href');

					var tbody = $('#currLoad');
					//if ($('#' + item.id)) { return;}

					if(a.html().indexOf('黄金') > -1) {
						tbody.prepend('<tr id="' + item.id + '"><td>' + new XDate(newDate).toString('HH:mm') + '</td><td style="color:red">' + a.html() + '</td></tr>');
					} else {
						tbody.prepend('<tr><td>' + new XDate(newDate).toString('HH:mm') + '</td><td>' + a.html() + '</td></tr>');
					}

				});
			} else {

			}
		});
	}

	function createLive(page, fun) {
		return;
		jq.getJSON('http://www.99990158.com/ajax/GetNewsLive.ashx?page=' + page, function(res) {
			if(maxId == 0) {
				maxId = _.max(res, function(chr) {
					return chr.id;
				}).id;

			}

			//假设你的列表返回在data集合中
			var list = _.sortBy(res, function(n) {
				return -n.createdAt;
			});

			//模板
			var t = document.getElementById('liveModel').innerHTML;
			var res = tmpl(t, list, tmpl_count(list), tmpCallBack);

			document.getElementById('LivePage').innerHTML += res;

			if(fun) {
				fun();
			}

			return;

			jq.each(list, function(index, item) {
				//加载数据 参数解释(模板，数据数量，回调函数[可以不传])；

			});

		});

	}

	function pullToRefreshArticle() {
		$(document.getElementById('ArticleList')).pullToRefresh({
			down: {
				callback: function() {
					console.log('down');
					var self = this;
					setTimeout(function() {
						$.getJSON('http://www.99990158.com/ajax/GetArticlePageList.aspx', {
							page: 1,
							size: 10,
							cid: 83,
							channel: 'news',
							sort: 'add_time desc'
						}, function(res) {
							$.each(res.data, function(index, item) {
								CreateA('ArticleList', item, 0);
							});
							self.endPullDownToRefresh();
						});

					}, 1000);
				}
			},
			up: {
				callback: function() {
					console.log('up');
					var self = this;
					setTimeout(function() {
						articlePage++;
						$.getJSON('http://www.99990158.com/ajax/GetArticlePageList.aspx', {
							page: articlePage,
							size: 10,
							cid: 83,
							channel: 'news',
							sort: 'add_time desc'
						}, function(res) {
							$.each(res.data, function(index, item) {
								CreateA('ArticleList', item, 1);
							});
							var more = jq('.mui-pull-bottom-tips');

							more.appendTo(jq('#ArticleList'));

							self.endPullUpToRefresh();
						});

					}, 1000);
				}
			}
		});
		return;
		//实时资讯
		var livePage = 1;
		$(document.getElementById('LivePage')).pullToRefresh({
			up: {
				callback: function() {
					console.log('up');
					var self = this;
					setTimeout(function() {
						livePage++;
						createLive(livePage, function() {
							self.endPullUpToRefresh();
						});

					}, 1000);
				}
			}
		});
	}

	function tmpCallBack(a, b) {

		switch(b[1]) {
			case "createdAt":
				var timestamp3 = a.createdAt;
				var newDate = new Date();
				newDate.setTime(timestamp3 * 1000);
				var newXDate = new XDate(newDate);
				var showTime = newXDate.toString('HH:mm');
				if(newXDate.toString('yyyy年MM月dd日') == new XDate().toString('yyyy年MM月dd日')) {
					//showTime = new XDate(newDate).toString('HH:mm');
				} else {

				}

				return showTime;
				break;
			case "contentText":
				var re = "";
				if(a.contentText.indexOf('黄金') > -1) {
					re = '<font style="color:red;">' + a.contentText + '</font>';
				} else {
					re = a.contentText;
				}

				return re;

				break;

			default:

				break;
		}
		return '';
	}

})(mui);

function Change(index) {
	console.log(11111)
	gallery.slider().gotoItem(index);
}